package com.example.projectmanagement.model.study;

import com.example.projectmanagement.model.User;
import jakarta.persistence.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import java.time.LocalDateTime;

@Entity
@Table(name = "study_question")
@Data
@NoArgsConstructor
@AllArgsConstructor
@EntityListeners(AuditingEntityListener.class)
public class Question {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false)
    private String title;

    @Column(length = 2000)
    private String content;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "user_id", nullable = false)
    private User user;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "course_id")
    private Course course;
    
    @Transient
    private Long courseId;

    @Column(name = "lesson_id")
    private Long lessonId; // 关联的课时ID或视频ID

    @Column(name = "view_count")
    private Integer viewCount = 0; // 浏览次数

    @Column(name = "answer_count")
    private Integer answerCount = 0; // 回答数量

    @Column(name = "like_count")
    private Integer likeCount = 0; // 点赞数

    @Column(name = "status")
    private String status; // 状态：开放/已解决/已关闭

    @CreatedDate
    @Column(name = "create_time", updatable = false)
    private LocalDateTime createTime;

    @LastModifiedDate
    @Column(name = "update_time")
    private LocalDateTime updateTime;
    
    // 添加getter方法来获取courseId
    public Long getCourseId() {
        if (this.courseId != null) {
            return this.courseId;
        }
        if (this.course != null) {
            return this.course.getId();
        }
        return null;
    }
    
    // 添加setter方法来设置courseId
    public void setCourseId(Long courseId) {
        this.courseId = courseId;
    }
}